<?php
/**
 *	Architect Framework
 *
 *	Architect Framework is a light weight object oriented web applications framework written for PHP 5.1.4 and later that provide the tools to develop and deploy a website or application fast and with as little code as possible.
 *
 *	@author Robin Grass, <hello@coffinboy.se>
 *	@link http://architect.kodlabbet.net
 *	@license http://www.opensource.org/licenses/gpl-3.0.html GNU General Public License (v. 3)
 */

/**
 *	Architect_Autoloader
 *
 *	This static class handles custom autoloading of classes via custom autoloader locators. These locator classes provides logic of how a class file should be loaded. For more information see Architect_Locator.
 *
 *	@see Architect_Locator
 *
 *	@package Core
 *	@version 1.0
 *	@author Robin Grass, <hello@coffinboy.se>
 */
class Architect_Autoloader {

	/**
	 *	@staticvar array $locators
	 */
	public static $locators = array();
	
	/**
	 *	registerLocator
	 *
	 *	Register a new locator to the Architect_Autoloader::$locators array.
	 *
	 *	@param object $locator Custom locator class object.
	 *	@param string $id Locator ID.
	 */
	public static function registerLocator(Architect_Locator $locator, $id) {
		self::$locators[$id] = $locator;
	}
	
	/**
	 *	unregisterLocator
	 *
	 *	Remove a locator from the Architect_Autoloader::$locators array, returns true 
	 *
	 *	@param string $id Locator ID.
	 */
	public static function unregisterLocator($id) {
		if(self::isRegisteredLocator($id)) {
			unset(self::$locators[$id]);
		return true;
		}
	return false;
	}
	
	/**
	 *	isRegisteredLocator
	 *
	 *	Checks if a locator exists in the Architect_Autoloader::$locators array.
	 *
	 *	@param string $id Locator ID.
	 *
	 *	@return bool
	 */
	public static function isRegisteredLocator($id) {
		return array_key_exists($id, self::$locators);
	}
	
	/**
	 *	load
	 *
	 *	This method loads a class based on the result returned by any of the active locators.
	 *	Developer's note; if a match is found before Architect_Autoloader::load has iterated through all of the locators, iteration stops where the match was found.
	 *
	 *	@param string $class Full class name to be loaded.
	 *
	 *	@return bool
	 */
	public static function load($class) {
		foreach(self::$locators as $locator) {
			if($locator->canLocate($class)) {
				require_once $locator->getPath($class);
				if(class_exists($class))
					return true;
			}
		}
	}
	
}
?>